package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorsMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private FlavorsMapper flavorsMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 根据id批量删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteById(List<Long> ids) {
        //判断菜品是否是停售状态
      List<Dish> dish =  dishMapper.seleteById(ids);
      log.info("根据id查询到的菜品为:{}",dish);
        for (Dish dish1 : dish) {
            if (dish1.getStatus() == StatusConstant.ENABLE){
               throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断菜品是否和套餐有关联
       List<Long> setmealIds =  setmealMapper.seleteByDishId(ids);
        log.info("根据菜品id查询到的套餐id为:{}",setmealIds);
        if ( !setmealIds.isEmpty() && setmealIds.size() != 0 ){
           throw  new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        dishMapper.deleteById(ids);
        //删除菜品的口味表
        flavorsMapper.deleleteByDishId(ids);
    }

    @Override
    /**
     * 新增菜品的逻辑处理
     *
     */
    @Transactional
    public void save(DishDTO dto) {
        Dish dish = new Dish();
        //进行数据的拷贝
        BeanUtils.copyProperties(dto, dish);
        //调用mapper的方法保存到菜品数据库
        dishMapper.insert(dish);
        //保存到口味数据库
        Long id = dish.getId();
        log.info("回填的菜品id为:{}", id);
        List<DishFlavor> flavors = dto.getFlavors();
        //判断集合是否为空
        if (flavors != null || flavors.size() !=0){
            //把菜品id加入到口味中去
            flavors.forEach(flavor -> flavor.setDishId(id));
            //存入口味表
            flavorsMapper.insertEatch(flavors);
        }
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult queryPage(DishPageQueryDTO dto) {
        //使用分页插件
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //查数据库
       Page<DishVO> dishPage = dishMapper.queryPage(dto);
       log.info("查询到的数据为:{}",dishPage.getResult());
        return new PageResult(dishPage.getTotal(),dishPage.getResult());
    }

    /**
     * 用户端根据分类id查询菜品及口味  条件查询
     * @return
     */
    @Transactional
    @Override
    public List<DishVO> getByCategoryIdList(Dish dish) {
      //先根据分类id到数据库中查询所有菜品及分类名称
        List<DishVO> dishList = dishMapper.list(dish);
        // List<DishVO> dishList =  dishMapper.seleteByCategoryIdaList(categoryId);
       log.info("根据分类id查询到的菜品dishvo对象属性为:{}",dishList);
      // log.info("查询到的菜品集合为:{}",dishList);
       if (dishList != null){
           //再根据菜品id查询其口味
           for (DishVO dishs : dishList) {
            List<DishFlavor>  dishFlavorList= flavorsMapper.seleteByDishId(dishs.getId());
            dishs.setFlavors(dishFlavorList);
           }
       }
       log.info("根据分类id最终查询到的菜品及口味集合为:{}",dishList);
        return  dishList;
    }

    /**
     * 根据分类id 条件查询到的所属菜品为
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> getAdminByCategoryIdList(Dish dish) {
       List<DishVO> dishVOList = dishMapper.list(dish);
        return dishVOList;
    }

    /**
     * 根据id查找菜品及所属口味
     * @param id
     */
    @Override
    public DishVO getById(Long id) {
        //查菜品表
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        List<Dish> dishList = dishMapper.seleteById(ids);
        log.info("根据单个菜品id查询到的菜品为:{}",dishList);
        //根据菜品id查找口味数据表
        List<DishFlavor> dishFlavorList = flavorsMapper.seleteByDishId(id);
        Dish dish = dishList.get(0);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO ;
    }

    /**
     * 修改菜品
     * @param dto
     */
    @Transactional
    @Override
    public void update(DishDTO dto) {
        //修改菜品表里面的数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.update(dish);
        //删除口味表数据 再添加
        Long dtoId = dto.getId();
        log.info("菜品id为:{}",dtoId);
        flavorsMapper.deleteBydishId(dtoId);
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors != null && flavors.size() >0){
            for (DishFlavor flavor : flavors) {
                log.info("口味集合中菜品idwei:{}",flavor.getDishId());
                flavor.setDishId(dtoId);
            }
            flavorsMapper.insertEatch(flavors);
        }
    }

    /**
     * 设置菜品的营业状态
     * @param status
     */
    @Override
    public void updateStatus(Integer status,Long id) {
       /*Dish dish = Dish.builder()
                        .status(status)
               .build();*/
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
    }
}
